//
//  NSDictionary+Handle.m
//  MovingLoaniOS
//
//  Created by RAIN on 2019/11/14.
//  Copyright © 2019 GWMFC. All rights reserved.
//

#import "NSDictionary+Handle.h"

@implementation NSDictionary (Handle)

- (NSString *)rg_stringForKey:(NSString *)key {
    id value = [self rg_valueForKey:key];
    NSString *string = [value rg_withoutNull];
    return string;
}

- (id)rg_valueForKey:(NSString *)key {
    id value;
    for (NSString *keyName in self.allKeys) {
        if ([keyName isEqualToString:key]) {
            // 如果找到了相应的key，递归就可以结束了
            value = self[keyName];
            break;
        } else {
            id tempObj = self[keyName];
            if ([tempObj isKindOfClass:[NSDictionary class]]) {
                // 如果是字典, 递归, 遍历下一层
                if ([value rg_withoutNull].length > 0) {
                    return value;
                } else {
                    value = [tempObj rg_valueForKey:key];
                }
            } else if ([tempObj isKindOfClass:[NSArray class]]) {
                // 如果是数组，只取第0个数据，并且传值递归
                if ([tempObj count] >= 1) {
                    for (NSInteger i = 0; i < [tempObj count]; i++) {
                        if ([value rg_withoutNull].length > 0) {
                            return value;
                        } else {
                            value = [tempObj[i] rg_valueForKey:key];
                        }
                    }
                }
            }
        }
    }
    return value;
}

@end
